package com.sinosoft.svc.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import smscommon.ComFun;
import smscommon.SmsClient;
import smsmessage.sms.DELIVER;

import com.sinosoft.svc.entity.SMSReceiveMassageEntity;
import com.sinosoft.svc.entity.SMSReceiveReportEntity;

public final class RCUtils {

	private static Logger logger = Logger.getLogger(RCUtils.class);

	public static long begin = 0;// 发送起始时间

	public static long rptBegin = 0;// 发送起始时间

	public static void receiveMessageOrReport(String realPath) throws Exception{
		SmsClient smsClient = SmsClient.getInstance();// /建立socket连接
		DELIVER deliver = new DELIVER();
		while (true) {

			synchronized (RCUtils.class) {
				if ((deliver = ComFun.getSMS()) != null) {
					if (deliver.getMsgFlag() == 0) {
						String filePath0 = null;

						if (deliver != null) {
							filePath0 = realPath + "logs/";
							File file = new File(filePath0);
							if (!file.exists()) {
								file.mkdirs();
							}
							SMSReceiveMassageEntity sMSReceiveMassageData = new SMSReceiveMassageEntity();
							String id = null;
							id = UUID.randomUUID().toString();
							sMSReceiveMassageData.setId(id);
							sMSReceiveMassageData.seteId(deliver.getSrcID());
							sMSReceiveMassageData.setMsg(deliver.getMsgContent());
							sMSReceiveMassageData.setRcvNo(deliver.getDestID());
							sMSReceiveMassageData.setRcvTime(new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(deliver.getAriveTime()));
							
							OutputStream out = new FileOutputStream(filePath0+"receiveMessage.txt", true);// 打印到文件
							OutputStreamWriter osr = new OutputStreamWriter(out);// 输出
							JSONObject jsonObject = JSONObject.fromObject(sMSReceiveMassageData);
							osr.write(jsonObject.toString() + ",");
							osr.flush();
							osr.close();

							logger.info("-上行短信：" + "上行源号码 ["
									+ deliver.getSrcID() + "]+" + " 上行目的号码 ["
									+ deliver.getDestID() + "]+" + " 上行消息内容 ["
									+ deliver.getMsgContent() + "]"
									+ "短信到达第三方接口的时间[" + deliver.getAriveTime()
									+ "]");

						}
					}
					if (deliver.getMsgFlag() == 1) {
						SMSReceiveReportEntity receiveReportInfoData = new SMSReceiveReportEntity();
						receiveReportInfoData.setId(UUID.randomUUID().toString());
						receiveReportInfoData.setSendId(deliver.getMsgContent());
						receiveReportInfoData.setTelephone(deliver.getSrcID());
						receiveReportInfoData.setReserve(deliver.getReserve());
						receiveReportInfoData.setReportState(deliver.getReportState());

						String filePath1 = null;
						filePath1 = realPath + "logs/";

						File file = new File(filePath1);
						if (!file.exists()) {
							file.mkdirs();
						}
						OutputStream out = new FileOutputStream(filePath1+"receiveReport.txt", true);// 打印到文件
						OutputStreamWriter osr = new OutputStreamWriter(out);// 输出
						JSONObject jsonObject = JSONObject
								.fromObject(receiveReportInfoData);
						/*
						 * 缓冲写 BufferedWriter bufw = new
						 * BufferedWriter(osr);//缓冲 bufw.write(str);
						 * bufw.flush(); bufw.close();
						 */
						osr.write(jsonObject.toString() + ",");
						osr.flush();
						osr.close();
						logger.info("状态报告:企业下行消息标识 [" + deliver.getMsgContent()
								+ "]" + " 手机号 [" + deliver.getSrcID() + "]"
								+ " 短信接收情况描述 [" + deliver.getReserve() + "]"
								+ " 消息推送状态[" + deliver.getReportState() + "]");

					}
				}
				try {
					RCUtils.class.wait(100);
				} catch (Exception e) {

				}
			}

		}
	}/* 接收end */

}